//
//  ToiletModel.m
//  Meroad
//
//  Created by 沈煜航 on 2019/2/19.
//  Copyright © 2019 沈煜航. All rights reserved.
//

#import "ToiletModel.h"

@implementation ToiletModel


#pragma 字典转模型

-(instancetype)initWithDict:(NSDictionary*)dict{
    if(self=[super init]){
        [self setValuesForKeysWithDictionary:dict];
        [self initialAddress];
    }
    return self;
}

+(instancetype)modelWithDict:(NSDictionary*)dict{
    return [[self alloc] initWithDict:dict];
}


#pragma 重写set方法

- (void)setLongitude:(CLLocationDegrees)longitude{
    _longitude = longitude;
    if(self.latitude){
        self.location = [[CLLocation alloc] initWithLatitude:_latitude longitude:_longitude];
    }
}

- (void)setLatitude:(CLLocationDegrees)latitude{
    _latitude = latitude;
    if(self.longitude){
        self.location = [[CLLocation alloc] initWithLatitude:_latitude longitude:_longitude];
    }
}

- (void)initialAddress{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:self.location completionHandler:^(NSArray<CLPlacemark* >* _Nullable placemarks, NSError* _Nullable error){
        NSLog(@"%f  %f",self.location.coordinate.longitude,self.location.coordinate.latitude);
        if(placemarks.count > 0){
            CLPlacemark* placemark = [placemarks objectAtIndex:0];
            if(placemark.administrativeArea != nil){
                self.address = [NSString stringWithFormat:@"%@",placemark.administrativeArea];
            }
            if(placemark.locality != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.locality];
            }
            if(placemark.subLocality != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.subLocality];
            }
            if(placemark.thoroughfare != nil && ![placemark.thoroughfare isEqualToString:placemark.name]){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.thoroughfare];
            }
            if(placemark.subThoroughfare != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.subThoroughfare];
            }
            if(placemark.name != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.name];
            }
            if(placemark.postalCode != nil){
                self.address = [NSString stringWithFormat:@"%@, %@",self.address,placemark.postalCode];
            }
            
            //NSLog(@"%@",self.address);
        }
    }];
}

-(NSInteger)leftPaper{
    return self.userEvaluation % 4;
}


#pragma others

-(BOOL)isEqual:(ToiletModel*)toilet{
    if(self.longitude == toilet.longitude
       && self.latitude == toilet.latitude
       && self.spareHolesNumber == toilet.spareHolesNumber
       && self.allHolesNumber == toilet.allHolesNumber
       && self.hygieneLevel == toilet.hygieneLevel
       && self.userEvaluation == toilet.userEvaluation
       && self.leftPaper == toilet.leftPaper
       ){
        return YES;
    }
    return NO;
}

-(void)initWithToiletModel:(ToiletModel*)toilet{
    //self.address = toilet.address;
    if(self.longitude!=toilet.longitude||self.latitude!=toilet.latitude){
        self.longitude = toilet.longitude;
        self.latitude = toilet.latitude;
        [self initialAddress];
    }
    else{
        if(self.address == nil && [self.address isEqualToString:@""]){
            [self initialAddress];
        }
    }
    self.spareHolesNumber = toilet.spareHolesNumber;
    self.allHolesNumber = toilet.allHolesNumber;
    self.hygieneLevel = toilet.hygieneLevel;
    self.userEvaluation = toilet.userEvaluation;
    self.leftPaper = toilet.leftPaper;
    
}
@end
